<template>
	<view>
		<!-- 扫码页面 -->
		<!-- #ifndef APP-PLUS -->
		<view class="wrap">
			<view class="u-tips-color">
				请在app中打开
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		checkStoreNum
	} from '@/common/api/inspectionAPI.ts'
	import {
		assetDetail
	} from "@/common/api/assetAPI.ts";
	export default {
		data() {
			return {
				barcode: null,
				flash: false,
				tip: '',
				successUrl: '',
				clickTipUrl: '',
				goBack:'',
				inspectParams:{},
			}
		},
		onShow() {
			// 页面展示时，重新启动扫描检测
			// if (this.barcode) {
				
				this.barcode.start()
			// }
		},
		onLoad(params) {
			const {
				tip,
				successUrl,
				clickTipUrl,
				goBack
			} = params
			if (tip && successUrl && clickTipUrl) {
				this.tip = tip
				this.successUrl = successUrl
				this.clickTipUrl = clickTipUrl
				this.goBack = goBack
			} else {
				uni.showToast({
					icon:'none',
					title:'调用扫码错误，需传tip(扫不出的提示信息),successUrl(扫码成功跳转的页面),clickTipUrl(点击扫不出的跳转的页面)三个参数！',
					duration:500
				})
				setTimeout(()=>{
					uni.navigateBack({
						delta: 1
					})
				},500)
			}
			// #ifdef APP-PLUS
			plus.navigator.setFullscreen(false); //全屏
			let currentWebview = this.$scope.$getAppWebview();
			this.createBarcode(currentWebview)
			this.createTipInfoView(currentWebview)
			this.createFlashBarView(currentWebview)
			// #endif
		},
		onBackPress() {
			if(this.successUrl == '/pages/assets/assetDetail/index'){
				uni.redirectTo({
					url:`/pages/assets/assetManage/index`
				})
				return true
			}
			
		},
		methods: {
			/**
			 * 创建二维码
			 * @param {Object} currentWebview
			 */
			createBarcode(currentWebview) {
				if (!this.barcode) {
					this.barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
						top: `0px`,
						left: '0px',
						height: `100%`,
						width: '100%',
						position: 'absolute',
						// background: '#FFCC00',
						frameColor: '#FFCC33',
						scanbarColor: '#FFCC33',
					});
					this.barcode.onmarked = this.onmarked;
					this.barcode.setFlash(this.flash);
					//此处未演示扫码成功回调的地址设置，实际请参考HTML5Plus API自行处理  
					//注意扫码区域需为正方形，否则影响扫码识别率  
					currentWebview.append(this.barcode);
				}
				this.barcode.start()
			},

			/**
			 * 创建提示信息
			 * @param {Object} currentWebview
			 */
			createTipInfoView(currentWebview) {
				const content = new plus.nativeObj.View('content', {
						bottom: '120px',
						left: '10%',
						height: '50px',
						width: '80%',
						backgroundColor: '#333',
						opacity: '.8',
						borderRadius: '20px'
					},
					[{
						tag: 'font',
						id: 'scanTips',
						text: this.tip,
						textStyles: {
							size: '18px',
							color: '#ffffff',
							whiteSpace: 'normal'
						},
						position: {
							top: '15px',
							left: '10%',
							width: '80%',
							height: 'wrap_content'
						}
					}]);
				currentWebview.append(content);

				content.addEventListener('click', () => {
					
					if(this.goBack){
						uni.navigateBack({
							delta:1
						})
					}else if(this.successUrl=='/pages/ipqc/submitInspect/index'){
						// console.log('***********',this.inspectParams);
						let obj = JSON.parse(this.clickTipUrl.split('=')[1])
						uni.redirectTo({
							url: `/pages/ipqc/submitInspect/index?inspRecordId=${obj.inspRecordId}&inspPositionId=${obj.inspPositionId}&version=${obj.version}&showType=${obj.showType}`,
						})
					}else if(this.successUrl=='/pages/checkAssets/checkResult/index'){
						uni.redirectTo({
							url: this.clickTipUrl
						})
					}else{
						uni.navigateTo({
							url: this.clickTipUrl
						})
					}
					
				})
			},
			// 创建 开关灯按钮
			createFlashBarView(currentWebview) {

				const openImg = this.crtFlashImg('static/yellow-scanBar.png')
				const closeImg = this.crtFlashImg('static/scanBar.png')
				const scanBarVew = new plus.nativeObj.View('scanBarVew', {
						top: '65%',
						left: '40%',
						height: '10%',
						width: '20%',
					},
					closeImg);
				scanBarVew.interceptTouchEvent(true);

				currentWebview.append(scanBarVew);

				scanBarVew.addEventListener("click", (e) => { //点亮手电筒
					this.flash = !this.flash;
					if (this.flash) {
						scanBarVew.draw(openImg);
					} else {
						scanBarVew.draw(closeImg)
					}
					if (this.barcode) {
						this.barcode.setFlash(this.flash);
					}
				}, false)
			},
			crtFlashImg(imgsrc) {
				return [{
					tag: 'img',
					id: 'scanBar',
					src: imgsrc,
					position: {
						width: '28%',
						left: '36%',
						height: '30%'
					}
				}, {
					tag: 'font',
					id: 'font',
					text: '轻触照亮',
					textStyles: {
						size: '16px',
						color: '#ffffff'
					},
					position: {
						width: '80%',
						left: '10%'
					}
				}]
			},
			// 扫码成功回调
			onmarked(type, result) {
				let obj = this.barcode
				console.log('条码类型：' + type);
				console.log('条码内容：' + result);
				// 业务代码
				// 核对扫描结果
				// 判断是否是正确的格式
				// 不正确则跳转到 错误页面
				if (this.clickTipUrl == '/pages/ipqc/index') {
					checkStoreNum(result).then(res => {
						if (res.code == 0) {
							uni.navigateTo({
								url: this.successUrl + '?qrCode=' + res.data.id
							})
						} else {
							uni.showToast({
								icon:'none',
								title:"数据获取失败，请不要扫描其他无关的二维码！"
							})
							setTimeout(()=>{
								obj.start()
							},1500)
						}
					})
				} else if (this.successUrl == '/pages/assets/assetDetail/index') {
					assetDetail(result).then((res) => {
						if (res.code == 0) {
							uni.navigateTo({
								url: this.successUrl + '?qrCode=' + JSON.stringify(res.data)
							})
						} else {
							uni.showToast({
								icon:'none',
								title:"数据获取失败，请不要扫描其他无关的二维码！"
							})
							setTimeout(()=>{
								obj.start()
							},1500)
						}
					});
				} else if (this.successUrl == '/pages/assets/assetRetrue/qrRetrue') {
					assetDetail(result).then((res) => {
						if (res.code == 0) {
							uni.navigateTo({
								url: this.successUrl + '?qrCode=' + JSON.stringify(res.data)
							})
						} else {
							uni.showToast({
								icon:'none',
								title:"数据获取失败，请不要扫描其他无关的二维码！"
							})
							setTimeout(()=>{
								obj.start()
							},1500)
						}
					});
				} else if (this.successUrl === '/pages/checkAssets/checkResult/index') {
					if (uni.getStorageSync('resultData').map(item => item.assetNo).includes(result)) {
						uni.showToast({
							icon:'none',
							title:"该资产已经被扫描放入列表了"
						})
						setTimeout(()=>{
							obj.start()
						},1500)
					} else {
						if (!uni.getStorageSync('checkData').map(item => item.assetNo).includes(result)) {
							uni.showToast({
								icon:'none',
								title:"请使用新增资产功能添加盘点列表不存在的资产"
							})
							setTimeout(()=>{
								obj.start()
							},1500)
						} else {
							uni.getStorageSync('checkData').forEach(item => {
								if (item.assetNo == result) {
									if (item.status !== 4) {
										item.status = 2
										item.statusCh = '盘点存在'
										uni.redirectTo({
											url: `/pages/checkAssets/checkResult/index?flag=${true}&scanData=${JSON.stringify(item)}`
										})
									} else { //扫出的资产是盘盈状态
										uni.showToast({
											icon:'none',
											title:"盘盈资产请不要重复添加"
										})
										setTimeout(()=>{
											obj.start()
										},1500)
									}
								}
							})
						}


					}

				}else if(this.successUrl=='/pages/ipqc/submitInspect/index'){
					let objParams = JSON.parse(this.clickTipUrl.split('=')[1])
					// this.inspectParams = obj
					if(result==objParams.inspPositionNo){
						uni.redirectTo({
							url: `/pages/ipqc/submitInspect/index?inspRecordId=${objParams.inspRecordId}&inspPositionId=${objParams.inspPositionId}&version=${objParams.version}&showType=${objParams.showType}`,
						})
						// uni.navigateTo({
						// 	url: `/pages/ipqc/submitInspect/index?inspRecordId=${objParams.inspRecordId}&inspPositionId=${objParams.inspPositionId}&version=${objParams.version}&showType=${objParams.showType}`,
						// })
					}else{
						uni.showToast({
							icon:'none',
							title:"数据获取失败，请不要扫描其他无关的二维码！"
						})
						setTimeout(()=>{
							obj.start()
						},1500)
					}
					
				}
			},

		}
	}
</script>

<style scoped>
	.wrap {
		height: calc(100vh);
	}
</style>
